package dst.ass1.jpa.model.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import dst.ass1.jpa.model.ITask;
import dst.ass1.jpa.model.ITaskProcessing;
import dst.ass1.jpa.model.ITaskWorker;
import dst.ass1.jpa.model.TaskStatus;


@Entity
public class TaskProcessing implements ITaskProcessing{

	private final static Logger LOGGER = Logger.getLogger(TaskProcessing.class.getName());
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	@Temporal(TemporalType.TIMESTAMP)   
	private Date start;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date end;
	
	@Enumerated(value = EnumType.STRING) //is an Enum
	private TaskStatus status;

	@ManyToMany(targetEntity=TaskWorker.class,cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch=FetchType.LAZY)  //to-star(collection) props are loaded lazily (as the number of items may well be very large)
	@JoinTable(name = "processing_taskworker",
    	joinColumns = @JoinColumn(name = "taskprocessings_id", referencedColumnName = "id"),
    	inverseJoinColumns = @JoinColumn(name = "taskworkers_id", referencedColumnName = "id"))
	private List<ITaskWorker> taskWorkers;
	
    @OneToOne(targetEntity = Task.class, 
    		cascade = { CascadeType.MERGE },
    		fetch=FetchType.EAGER) //to-one properties are loaded eagerly (as loading them is cheap)
	private ITask task;
	
	public TaskProcessing (){
		this.taskWorkers= new ArrayList<ITaskWorker>();
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Date getStart() {
		return start;
	}
	public void setStart(Date start) {
		this.start = start;
	}
	public Date getEnd() {
		return end;
	}
	public void setEnd(Date end) {
		this.end = end;
	}
	public TaskStatus getStatus() {
		return status;
	}
	public void setStatus(TaskStatus status) {
		this.status = status;
	}
	public List<ITaskWorker> getTaskWorkers() {
		return taskWorkers;
	}
	public void setTaskWorkers(List<ITaskWorker> list) {
		this.taskWorkers = list;
	}
	
	@Override
	public void addWorker(ITaskWorker worker) {
		if(this.taskWorkers!=null){
			this.taskWorkers.add(worker);
		}
		else{
			LOGGER.info("TaskProcessing.List<ITaskWorker> was null");
		}
	}
	
	public ITask getTask() {
		return task;
	}
	public void setTask(ITask task) {
		this.task = task;
	}
	
	
	
	
	

}
